package hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.omg.CORBA_2_3.portable.InputStream;

import javax.servlet.http.HttpServlet;
import java.io.*;
import  java.util.*;
import java.io.InputStreamReader;

public class table {

    public static void main(String[] args) throws IOException {

        File file=new File("E:\\project9\\src\\pro9.txt");
        if(file.isFile() && file.exists()){
            InputStreamReader read = new InputStreamReader( new FileInputStream(file));
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            //在这里连接hbase
            //  创建HBase配置对象
            Configuration configuration = HBaseConfiguration.create();
            // 指定zookeeper集群地址
            configuration.set("hbase.zookeeper.quorum", "192.168.116.11");
            // 创建连接对象
            Connection connection = ConnectionFactory.createConnection(configuration);
            // 获取Table对象,Table负责与记录相关的操作,如增删改查等
            Table table = connection.getTable(TableName.valueOf("goodsmessage"));

            //获取每一行的数据并进行分割
            while((lineTxt = bufferedReader.readLine()) != null){

                String[] split = lineTxt.split("\\s+");
                String id1= split[0];
                String name1 = split[1];
                String price1 = split[2];
                String time1 = split[3];
                String status1 = split[4];
                //在这里用put把这一行的每一列put进hbase
                Put put1 = new Put(Bytes.toBytes(id1));
                put1.add(Bytes.toBytes("a"),
                        Bytes.toBytes("id"),
                        Bytes.toBytes(id1));
                put1.add(Bytes.toBytes("b"),
                        Bytes.toBytes("name"),
                        Bytes.toBytes(name1));
                put1.add(Bytes.toBytes("c"),
                        Bytes.toBytes("price"),
                        Bytes.toBytes(price1));
                put1.add(Bytes.toBytes("d"),
                        Bytes.toBytes("time"),
                        Bytes.toBytes(time1));
                put1.add(Bytes.toBytes("e"),
                        Bytes.toBytes("status "),
                        Bytes.toBytes(status1));
                table.put(put1);


            }
            bufferedReader.close();
            read.close(); }
        else{
            System.out.println("找不到指定的文件");
        }
    }
    }



